﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using System.Threading;
using System.Web.Routing;
using Tasks.Business;

namespace Tasks.Core
{
    /// <summary>
    /// 需要登录的属性，用于管理控制器的权限
    /// </summary>
    public class RequireLoginAttribute : AuthorizeAttribute
    {
        public string Module { get; set; }
        public string Action { get; set; }

        public RequireLoginAttribute()
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="module">所属模块</param>
        /// <param name="action">所属动作</param>
        public RequireLoginAttribute(string module, string action)
        {
            this.Module = module;
            this.Action = action;
        }

        /// <summary>
        /// 重写OnAuthorization
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (Config.CurrentOperator == null)
            {
                ControllerBase ctr = filterContext.Controller;

                filterContext.Result = new RedirectToRouteResult("default",
                    new RouteValueDictionary(new { controller = "Home", action = "Index" }));
            }

            HttpContextBase context = filterContext.HttpContext;
            //string path = context.Request.Path;
            Power power = new Power();
            //if (!power.ValidateUrl(path))
            //{
            //    context.Response.Write("没有访问权限");
            //    context.Response.End();
            //}

            if (
                (!string.IsNullOrEmpty(this.Module) && !string.IsNullOrEmpty(this.Action))
                &&
                !power.ValidatePower(this.Module, this.Action))
            {
                context.Response.Write("没有访问权限");
                context.Response.End();
            }

        }
    }

}
